package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.oss.core.OssClient;
import com.ruoyi.system.domain.ArchiveLocation;
import com.ruoyi.system.domain.bo.ArchiveLocationBo;
import com.ruoyi.system.domain.vo.ArchiveLocationVo;
import com.ruoyi.system.enums.ArchiveLocationTypeEnum;
import com.ruoyi.system.mapper.ArchiveLocationMapper;
import com.ruoyi.system.service.IArchiveLocationService;
import com.ruoyi.system.util.ArchiveUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 归档位置Service业务层处理
 *
 * @author admin
 * @date 2024-12-05
 */
@RequiredArgsConstructor
@Service
public class ArchiveLocationServiceImpl implements IArchiveLocationService {

    private final ArchiveLocationMapper baseMapper;

    /**
     * 查询归档位置
     */
    @Override
    public ArchiveLocationVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询归档位置列表
     */
    @Override
    public TableDataInfo<ArchiveLocationVo> queryPageList(ArchiveLocationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArchiveLocation> lqw = buildQueryWrapper(bo);
        Page<ArchiveLocationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询归档位置列表
     */
    @Override
    public List<ArchiveLocationVo> queryList(ArchiveLocationBo bo) {
        LambdaQueryWrapper<ArchiveLocation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ArchiveLocation> buildQueryWrapper(ArchiveLocationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArchiveLocation> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ArchiveLocation::getName, bo.getName());
        lqw.eq(bo.getLocationType() != null, ArchiveLocation::getLocationType, bo.getLocationType());
        lqw.eq(bo.getPubStatus() != null, ArchiveLocation::getPubStatus, bo.getPubStatus());
        return lqw;
    }

    /**
     * 新增归档位置
     */
    @Override
    public Boolean insertByBo(ArchiveLocationBo bo) {
        ArchiveLocation add = BeanUtil.toBean(bo, ArchiveLocation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改归档位置
     */
    @Override
    public Boolean updateByBo(ArchiveLocationBo bo) {
        ArchiveLocation update = BeanUtil.toBean(bo, ArchiveLocation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ArchiveLocation location) {
        ArchiveUtil.checkLocation(location);
    }

    /**
     * 批量删除归档位置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean checkLoaction(ArchiveLocationBo bo) {
        ArchiveLocation update = BeanUtil.toBean(bo, ArchiveLocation.class);
        validEntityBeforeSave(update);
        return Boolean.TRUE;
    }
}
